import datetime,requests,json,calendar
from datetime import date, timedelta, datetime as diuu

def get_Salestrend():
    today = datetime.date.today().strftime('%Y%m%d')
    today2 = datetime.date.today()
    now = datetime.date.today()
    this_month_start = datetime.datetime(now.year, now.month, 1).strftime('%Y/%m/%d')
    this_month_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1]).strftime('%Y/%m/%d')
    last_day_of_last_month = datetime.date(today2.year, today2.month, 1) - datetime.timedelta(1)
    first_day_of_last_month = datetime.date(last_day_of_last_month.year, last_day_of_last_month.month, 1)
    loginurl='https://auth.xincheng.com/LoginService.ashx'
    listurl='http://passport.pri.xincheng.com/subsystems.do'
    logindata = {
        'lang': 'en - us',
        'systemCode': 'A07',
        'rememberPassword': 'false',
        'challengeNumber': '',
        'randomDate': '2021-03-18 14:52:22',
        'hidetxtPassword': 'Ou/On8kXuyEZHHZaAvbl6AKHPnNTw+UmS3bmbDUvKrQ7G1Wk4JAUrFS+wzlwIoEV/3pbUDvwmTT+iyzlQqqMnLpxTaKM7368C5a5L18JctGAavs43U89FaBOjAXd259cCHYx4TsAPzC9RQH18YZTUlNiUJeuL8L9ps915yF6p1STW6WUUP5Jvglt6xL0OvwCzi9Ai2J+L1Izl5h5B+LfdtwIV2NZ2zF8V1GdK2Cimuxf3uKkQx+FnQHc5qK/U7zsgkpLtMSaiaGAarhJ8tni/DOZmbup14ZtzxJUFNFPHkT64zvULgO4ilyvCWgWBZvrhUmxwliemxQtRiPXZSU/pA==',
        'txtIsTrustAccessor': 'True',
        'txtUserName': 'peiningyong',
    }
    loginheader={
        'Referer': 'https://auth.xincheng.com/?challengeNumber=ZGY5eGpsaGt5NTB0cWRlcHJ0c2h1OXVuMDBlMHFnbXMwN2JmZGEzYmEyNTlsaHFtYXN5MHlheWRjOGNsZmIyZjF4ejJ0b3BvbW1mMHl4ZTR4b2ZlcnlrdGVsemRncG1yY3MybzVicmxyOHlqdDRsbTgzb2I5d2tpOGx3dnlvMW9zOXJ6azh4OWRoYnVkd253b2pubHNpbmN2Y3ZvOWttNjBtcW14MWV2NDFnOHpmc2s4bHN2ZmN3aGk0Y2RhZXR4c2JtbjJmYmZ6cWV1MWlibnE2OGUyOTd4azBpMGd5ODlrMmFqM2w3djMxb2d5c3N1bnFnYWFjNHV6YWluZWY5cA==&flag=3&systemCode=A07&RetutnUrl=http%3A%2F%2Fekp.xincheng.com%2Fsys%2Fportal%2Fpage.jsp',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36',
    }
    header={
        'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_15_6) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 84.0.4147.89Safari / 537.36',
        'Referer': 'http://databi.pri.xincheng.com/',
    }
    session=requests.session()
    session.post(loginurl,data=logindata,headers=loginheader)
    tokenreq=session.get(listurl)
    tokenreq.content.decode()
    tokenreq=json.loads(tokenreq.text)
    tokenreq=tokenreq['body']['user']['orgId']
    monthurl = 'http://databiapi.web.pri.xincheng.com/shop/global/getShopSaleInfo?token=misUser' + tokenreq + ''
    data3={
        'id': tokenreq,
        'from': str(this_month_start),
        'to': str(this_month_end),
        'floors':'', 
        'shopTypes': '',
        'shopEntityIds': '',
        'pageIndex': 1,
        'pageSize': 500,
        'type': 0,
        'sums': 'saleDate,netSale,saleAmount,saleCount,billAvg,refundSaleAmount,refundSaleCount,claimMoneyTotal',
        'timeType': 'd',
        'orderBy': ''
    }
    data4={
        'id': tokenreq,
        'from': str(first_day_of_last_month),
        'to': str(last_day_of_last_month),
        'floors':'', 
        'shopTypes': '',
        'shopEntityIds': '',
        'pageIndex': 1,
        'pageSize': 500,
        'type': 0,
        'sums': 'saleDate,netSale,saleAmount,saleCount,billAvg,refundSaleAmount,refundSaleCount,claimMoneyTotal',
        'timeType': 'd',
        'orderBy': ''
    }
    thismonth = []
    thatmonth = []
    thismonthdata=session.post(monthurl,headers=header,data=data3)
    thismonthdata.content.decode()
    thismonthdata=json.loads(thismonthdata.text)
    thismonthdata = thismonthdata['data']['data']
    for i in thismonthdata:
        thismonth.append('{:.2f}'.format(i['claimMoneyTotal']/10000))

    thatmonthdata=session.post(monthurl,headers=header,data=data4)
    thatmonthdata.content.decode()
    thatmonthdata=json.loads(thatmonthdata.text)
    thatmonthdata=thatmonthdata['data']['data']
    for i in thatmonthdata:
        thatmonth.append('{:.2f}'.format(i['claimMoneyTotal']/10000))
    return thismonth,thatmonth

def days_cur_month():#获取当月所有日期
    m = diuu.now().month
    y = diuu.now().year
    if m == 12:
        m = 11
        ndays = (date(y, m+1, 1) - date(y, m, 1)).days
        d1 = date(y, 12, 1)
        d2 = date(y, 12, ndays)
        delta = d2 - d1
        return [(d1 + timedelta(days=i)).strftime('%-m月%-d日') for i in range(delta.days + 2)]
    ndays = (date(y, m + 1, 1) - date(y, m, 1)).days
    d1 = date(y, m, 1)
    d2 = date(y, m, ndays)
    delta = d2 - d1
    return [(d1 + timedelta(days=i)).strftime('%-m月%-d日') for i in range(delta.days + 1)]

if __name__ == "__main__":
    get_Salestrend()